package pers.wangyu.crawler.service.impl;

import com.google.common.collect.Lists;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import org.apache.commons.lang3.Validate;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.wangyu.crawler.entity.Book;
import pers.wangyu.crawler.repository.BookRepository;
import pers.wangyu.crawler.service.BookService;

/**
 * 书籍服务层实现
 *
 * @author wangyu
 * @Date 2018-05-05 23:38
 */
@Service("bookService")
public class BookServiceImpl implements BookService {

  @Autowired
  private BookRepository bookRepository;

  @Override
  public Book saveBook(Book book) {
    Validate.notNull(book, "书籍信息不能为空");
    return bookRepository.save(book);
  }

  @Override
  public List<String> downloadBookUrl(String keyWord) {
    List<String> bookUrls = Lists.newArrayList();
    int index = 0;
    try {
      //URLEncoder编码,采用UTF-8模式
      keyWord = URLEncoder.encode(keyWord, "UTF-8");
      while (true) {
        Document doc = Jsoup
            .connect("https://book.douban.com/tag/" + keyWord + "?start=" + index + "&type=T")
            .header("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)")
            .timeout(3000).get();//设置连接超时时间
        Elements newsHeadlines = doc.select("ul").select("h2").select("a");
        System.out.println("本页数量：  " + newsHeadlines.size());
        for (Element e : newsHeadlines) {
          System.out.println(e.attr("href"));
          bookUrls.add(e.attr("href"));
        }
        index += newsHeadlines.size();
        System.out.println("共抓取url个数：" + index);
        if (newsHeadlines.size() == 0) {
          System.out.println("--------------The end--------------");
          break;
        }
        //暂时只抓取前10页，共200条数据
        if (index == 100) {
          break;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return bookUrls;
  }

  @Override
  public void getBookInfo(List<String> bookUrl) {
    for (String url : bookUrl) {
      try {
        Document doc = Jsoup
            .connect(url)
            .header("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)")
            .timeout(3000).get();//设置连接超时时间

        //解析网页相关信息
        Elements titleElement = doc.select("div.subject.clearfix").select("a");
        Elements scoreElement = doc.getElementsByClass("rating_self").select("strong");
        Elements RatingSumElement = doc.getElementsByClass("rating_sum").select("a")
            .select("span");
        Elements authorElement = doc.getElementById("info").select("span").first().select("a");
        Element pressElement = doc.getElementById("info");

        //书名
        String bookName = titleElement.attr("title");
        //评分
        String score = scoreElement.html();
        //评价人数
        String RatingSum = RatingSumElement.html();
        //作者
        String author = authorElement.html();

        String press = pressElement.html();
        //出版社
        if (press.indexOf("出版社") > -1) {
          press = pressElement.text().split("出版社:")[1].split(" ")[1];
        } else {
          press = "";
        }
        //出版日期
        String date = pressElement.html();
        if (date.indexOf("出版年") > -1) {
          date = pressElement.text().split("出版年")[1].split(" ")[1];
        } else {
          date = "";
        }
        // 价格
        String price = pressElement.text();
        if (price.indexOf("定价:") > -1) {
          price = pressElement.text().split("定价:")[1].split(" ")[1];
          if (price.equals("CNY")) {
            price = pressElement.text().split("定价:")[1].split(" ")[2];
          }
        } else {
          price = "";
        }

        System.out.println("-------------------------------------------------------");
        System.out.println("书名：" + bookName);
        System.out.println("评分：" + score);
        System.out.println("评价人数：" + RatingSum);
        System.out.println("作者：" + author);
        System.out.println("出版社：" + press);
        System.out.println("出版日期：" + date);
        System.out.println("价格：" + price);
        System.out.println("-------------------------------------------------------");

        //根据需求设定需要爬取的条件，例如：评分在9.0以上的书籍，然后存入数据库
        if (!"".equals(score) && Double.parseDouble(score) >= 9.0) {
          Book book = new Book();
          book.setBookName(bookName);
          book.setAuthor(author);
          book.setPress(press);
          book.setPublicationDate(date);
          book.setScore(Double.parseDouble(score));
          book.setRatingSum(Long.parseLong(RatingSum));
          book.setPrice(price);
          //保存
          bookRepository.save(book);
          System.out.println("success!");
        }

        //睡眠1秒,防止豆瓣封禁IP的反爬虫行为
        try {
          Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
